package com.tyl.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tyl.ResultEntity;
import com.tyl.entity.Admin;
import com.tyl.entity.Role;
import com.tyl.service.IAdminService;
import com.tyl.service.IMenuService;
import org.apache.catalina.User;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 汤云龙
 * @since 2022-03-17
 */
@RestController
@RequestMapping("/admin")
public class AdminController {
    @Resource
    private IAdminService adminService;
    @GetMapping("showAll")
    public ResultEntity ShowAll(){
        List<Admin> list = adminService.list();
        list.forEach(System.out::println);
        return ResultEntity.ok(list);
    }
    @PostMapping("insert")
    public ResultEntity InsertUser(@RequestBody Admin admin){
        adminService.save(admin);
        return ResultEntity.ok();
    }
    @DeleteMapping("delete/{id}")
    public ResultEntity deleteById(@PathVariable("id") Integer id){
        adminService.removeById(id);
        return ResultEntity.ok();
    }
    @GetMapping("selectById/{userId}")
    public ResultEntity selectById(@PathVariable("userId") Integer id){
        Admin admin = adminService.getById(id);
        System.out.println(admin);
        return ResultEntity.ok(admin);
    }
    @GetMapping("/selectRoleId/{userId}")
    public ResultEntity selectUserId(@PathVariable("userId") Integer id){
        //根据用户id查询用户对应了那些角色
        List<Role> roles = adminService.selectByAdminIdAndRoleId(id);
        System.out.println(roles);
        return ResultEntity.ok(roles);
    }
    @PutMapping("/update")
    public ResultEntity updateAdmin(@RequestBody Admin admin){
        //修改用户
        QueryWrapper<Admin> wrapper = new QueryWrapper<>();
        wrapper.eq("id",admin.getId());
        adminService.update(admin,wrapper);
        return ResultEntity.ok();
    }
}

